﻿#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#define MAX_LENGTH 1024
#define SEND_LENGTH 512

/*******************************************************************************
  函 数 名		:  
  功能描述		:域套接字客户端样例代码,自己本地搭建一个简单的SpringBoot微服务;
				代码将请求http://3.200.2.142:8000/index发给服务端域套接字/tmp/http_proxy_connect.socket，
				ngnix代理监听发送到该套接字的请求并负责转发,返回结果。
				注:请求信息结构要符合http请求头格式
  输入参数		:  None
  输出参数		:  None
  返 回 值		:  None
*******************************************************************************/
int main(int argc,char *argv[])
{
	printf("the client is in\n");
	int sockfd,len,result,byte;
	struct sockaddr_un address;
	//创建socket，指定通信协议为AF_UNIX,数据方式SOCK_STREAM
	if((sockfd = socket(AF_UNIX, SOCK_STREAM, 0))==-1)
	{
		perror("socket");
		exit(EXIT_FAILURE);
	}
	
	//配置server_address
	address.sun_family = AF_UNIX;
	//将地址绑定到nginx监听的域套接字/tmp/http_proxy_connect.socket
	strcpy(address.sun_path, "/tmp/http_proxy_connect.socket");
	len = sizeof(address);
 	printf("The client begin to connect service\n");
	//建立连接
	result = connect(sockfd, (struct sockaddr *)&address, len);
	printf("The connect result from service is %d, %s\n",result, strerror(errno));
	if(result == -1) 
	{
		printf("ensure the server is up\n");
		perror("connect");
		exit(EXIT_FAILURE);
	}
	/*---------------------------------------开始CONNECT连接----------------------------------*/
	//要发给域套接字的请求信息
	char connectUrl[] = "CONNECT 3.200.6.125:8000 HTTP/1.1\r\nHost:3.200.6.125:8000\r\n\r\n";
	//存放发送的信息
	char send_buf_connect[SEND_LENGTH];
	//存放收到的信息
	char ch_recv_connect[MAX_LENGTH];
	//用sprintf事先把消息写到send_buf_connect
	sprintf(send_buf_connect,connectUrl);
	printf("The send message is %s and length is %d\n",send_buf_connect,strlen(send_buf_connect));
	//发送请求数据
	if((byte=write(sockfd, send_buf_connect, strlen(send_buf_connect)))==-1)
	{
		perror("write");
		exit(EXIT_FAILURE);
	}
	//接收返回数据
	if((byte=read(sockfd,&ch_recv_connect,MAX_LENGTH))==-1)
	{
		perror("read");
		exit(EXIT_FAILURE);
	}
	printf("receive from server data is: %s\n",ch_recv_connect);

	/*---------------------------------------CONNECT 连接成功----------------------------------*/
	
	/*---------------------------------------开始发送get请求----------------------------------*/
	//要发给代理的请求信息
	char getUrl[] = "GET /index HTTP/1.1\r\nProxy-Connection:keep-alive\r\nHost:3.200.6.125:8000\r\n\r\n";
	//存放发送的信息
	char send_buf_get[SEND_LENGTH];
	//存放收到的信息
	char ch_recv_get[MAX_LENGTH];
	//用sprintf事先把消息写到send_buf
	sprintf(send_buf_get,getUrl);
	printf("The send message is %s and length is %d\n",send_buf_get,strlen(send_buf_get));
	if((byte=write(sockfd, send_buf_get, strlen(send_buf_get)))==-1)
	{
		perror("write");
		exit(EXIT_FAILURE);
	}
	if((byte=read(sockfd,&ch_recv_get,MAX_LENGTH))==-1)
	{
		perror("read");
		exit(EXIT_FAILURE);
	}
	printf("The result from service is: %s\n",ch_recv_get);
	/*---------------------------------------get请求结束----------------------------------*/
	close(sockfd);
	return 0;
}